package com.yunc.modules.quartz.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrSpliter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.utils.R;
import com.yunc.modules.quartz.entity.SysJobLog;
import com.yunc.modules.quartz.service.ISysJobLogService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定时任务日志
 *
 * @author congcong tao
 * @create 2018/11/21 下午8:08
 */
@RestController
@RequestMapping("sysJobLog")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysJobLogController {

    private final ISysJobLogService sysJobLogService;
    
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermi('sys:job:log:list')")
    public R page(Page<SysJobLog> page, SysJobLog sysJobLog){
        QueryWrapper<SysJobLog> ew = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(sysJobLog.getJobName())) {
            ew.eq("job_name", sysJobLog.getJobName());
        }
        ew.orderByDesc("create_time");
        sysJobLogService.page(page, ew);
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @DeleteMapping("/delete/{ids}")
    @PreAuthorize("@ss.hasPermi('sys:job:log:delete') AND @ss.hasRole('1')")
    public R info(@PathVariable String ids) {
        List<String> idsList = StrSpliter.splitTrim(ids, ',', true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        boolean flag = sysJobLogService.removeByIds(idsList);
        if (flag) {
            return R.ok("删除成功");
        }
        return R.error("删除失败");
    }

}
